package com.easy.sftp.pool;

import com.easy.sftp.config.BaseConfig;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

/**
 * @Description: 管道父类
 * @Author: wxl
 * @CreateTime: 2022/9/15 23:52
 */
public class ChannelFactory<T extends BaseConfig, E> extends BasePooledObjectFactory<E> {

    private T configBean;

    public ChannelFactory (T configBean) {
        this.configBean = configBean;
    }

    @Override
    public E create() throws Exception {
        return Connection.connect(configBean.getUsername(),
                configBean.getIp(),
                configBean.getPassword(),
                configBean.getPort(),
                configBean.getType());
    }

    @Override
    public PooledObject<E> wrap(E e) {
        return new DefaultPooledObject<>(e);
    }

    @Override
    public void destroyObject(PooledObject<E> p, DestroyMode destroyMode) throws Exception {
        super.destroyObject(p, destroyMode);
    }
}
